//
//  FirstViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "FirstViewController.h"
#import "XDHeader.h"
#import "XDTools.h"
#import "UIButton+Category.h"
#import "UIImageView+WebCache.h"
#import "SettingsViewController.h"
#import "TasteTypeViewController.h"
#import "LoginViewController.h"
#import "OrderListViewController.h"
#import "PayBackMoneyViewController.h"
#define image_tag 10000

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (ISLOGING) {
        titleIV.hidden = YES;
        titleIV1.hidden = NO;
        userNameLB.hidden = NO;
        userNameLB.text = [[NSUserDefaults standardUserDefaults] objectForKey:kMMyUserInfo][0];
        userCountLB.hidden = NO;
        [self chaxun];

    }else{
        titleIV.hidden = NO;
        titleIV1.hidden = YES;
        userNameLB.hidden = YES;
        userCountLB.hidden = YES;
    }
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.leftBtn.hidden =YES;

    [self changeFrameWhenHiddenNavigationBar];
    
//    [[XDTools sharedXDTools] checkVersion:self isShowAlert:YES];
    
//    [[[NSUserDefaults standardUserDefaults] objectForKey:kMMyUserInfo] removeAllObjects];
    
    backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT)];
    backScrollView.delegate = self;
    backScrollView.contentSize = CGSizeMake(UI_SCREEN_WIDTH, 576);
    [self.contentView addSubview:backScrollView];
    
    int h = 0;
    if (!IOS7) {
        h = 20;
    }
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, h, UI_SCREEN_WIDTH, 190)];
    bgView.backgroundColor = RGBA(241, 141, 0, 1);
    [backScrollView addSubview:bgView];
    
    UIImageView * IV1 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 49/2.0f, 49/2.0f)];
    IV1.image = [UIImage imageNamed:@"home_02_bai"];
    [backScrollView addSubview:IV1];
    
    UIImageView * IV2 = [[UIImageView alloc] initWithFrame:CGRectMake(280, 15, 49/2.0f, 49/2.0f)];
    IV2.image = [UIImage imageNamed:@"setting"];
    [backScrollView addSubview:IV2];
    
    [XDTools getAButtonForView:backScrollView frame:CGRectMake(0, 0, 50, 44) nomalTitle:nil hlTitle:nil titleColor:nil bgColor:[UIColor clearColor] nbgImage:nil hbgImage:nil action:@selector(goToPersonCenter) target:self buttonTpye:UIButtonTypeCustom];
    
    [XDTools getAButtonForView:backScrollView frame:CGRectMake(270, 0, 50, 44) nomalTitle:nil hlTitle:nil titleColor:nil bgColor:[UIColor clearColor] nbgImage:nil hbgImage:nil action:@selector(goToSettingCenter) target:self buttonTpye:UIButtonTypeCustom];
    
    
    
    userNameLB = [XDTools addAlabelForAView:backScrollView withText:@"13911088888" frame:CGRectMake(90, 130, 140, 20) font:[UIFont systemFontOfSize:20] textColor:[UIColor whiteColor]];
    userNameLB.textAlignment = NSTextAlignmentCenter;
    
    userCountLB = [XDTools addAlabelForAView:backScrollView withText:@"账户余额" frame:CGRectMake(90, 150, 140, 20) font:[UIFont systemFontOfSize:14] textColor:[UIColor whiteColor]];
    userCountLB.textAlignment = NSTextAlignmentCenter;
    
    titleIV = [[UIImageView alloc] initWithFrame:CGRectMake((UI_SCREEN_WIDTH-311/2.0f)/2.0f, 77, 311/2.0f, 100/2.0f)];
    titleIV.image = [UIImage imageNamed:@"mainLogo"];
    [backScrollView addSubview:titleIV];
    
    titleIV1 = [[UIImageView alloc] initWithFrame:CGRectMake((UI_SCREEN_WIDTH-143/2.0f)/2.0f, 50, 143/2.0f, 143/2.0f)];
    titleIV1.image = [UIImage imageNamed:@"login_logo"];
    [backScrollView addSubview:titleIV1];
    
    UIImageView * lineIV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 355, UI_SCREEN_WIDTH - 20, .5f)];
    lineIV.image = [UIImage imageNamed:@"line"];
    [backScrollView addSubview:lineIV];
    
    [XDTools getAButtonForView:backScrollView frame:CGRectMake(119, 215, 267/2.0f, 245/2.0f) nomalTitle:nil hlTitle:nil titleColor:nil bgColor:nil nbgImage:@"zhifu" hbgImage:@"zhifu" action:@selector(goToTry) target:self buttonTpye:UIButtonTypeCustom];
    
    [XDTools getAButtonForView:backScrollView frame:CGRectMake(119, 383, 270/2.0f, 248/2.0f) nomalTitle:nil hlTitle:nil titleColor:nil bgColor:nil nbgImage:@"huankuan" hbgImage:@"huankuan" action:@selector(payBackMoney) target:self buttonTpye:UIButtonTypeCustom];

}

- (void)chaxun{
    if ([XDTools NetworkReachable]){

        NSString * userId = [[NSUserDefaults standardUserDefaults] objectForKey:kMMyUserInfo][0];

        NSDictionary * dic = @{@"merId":@"1000002084",
                               @"opCode": @"20A00H",
                               @"userId":userId};

        DDLOG(@"dic ::::::  %@",dic);

        NSArray * arr = [NSArray arrayWithObjects:@"merId",@"opCode",@"userId", nil];


        __weak ASIHTTPRequest * request = [XDTools getRequestWithDict:dic array:arr];
        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [request responseString];

            DDLOG(@"chaxun  responseString:%@", responseString);
            if ([responseString rangeOfString:@"000000"].length == 6) {
                //保存paytoken
                DDLOG(@"%@",responseString);
                NSRange range1 = [responseString rangeOfString:@"usingAmt="];
                NSRange range2 = [responseString rangeOfString:@"&sign="];
                int payNum = [[responseString substringWithRange:NSMakeRange(range1.location + range1.length, range2.location - range1.location - 9)] intValue];
                userCountLB.text = [NSString stringWithFormat:@"账户余额:%.2f元",payNum/100.0f];
            }
        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",request.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];


    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
}

- (void)goToPersonCenter{
    if (ISLOGING) {
        OrderListViewController * orders = [[OrderListViewController alloc] init];
        [self.navigationController pushViewController:orders animated:YES];
    }else{
        [XDTools showTips:@"请先体验支付" toView:self.view];
    }
}

- (void)goToSettingCenter{
    SettingsViewController * setting = [[SettingsViewController alloc] init];
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)goToTry{
    TasteTypeViewController * taste = [[TasteTypeViewController alloc] init];
    [self.navigationController pushViewController:taste animated:YES];
}

- (void)payBackMoney{
    PayBackMoneyViewController * payBack = [[PayBackMoneyViewController alloc] init];
    [self.navigationController pushViewController:payBack animated:YES];
}


-(void)clearClick
{
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
}

-(void)btnClick:(UIButton *)sender
{
    DDLOG_CURRENT_METHOD;
    if (sender.tag==1){
        [XDTools showTips:@"操作成功" toView:self.contentView];
    }else if (sender.tag ==2){
        [XDTools showProgress:self.contentView];
    }else if (sender.tag ==3){
        [XDTools showProgress:self.contentView showText:@"正在加载..."];
    }else if (sender.tag ==4){
    
    }else if (sender.tag ==5){
    
    }
}

-(void)getData
{
    NSDictionary *dict = @{@"module":@"1",
                           @"page":@"1",
                           };
    
    __weak ASIHTTPRequest *request = [XDTools getRequestWithDict:dict API:API_GetInfoList];
    [request setCompletionBlock:^{
        NSString *responseString = [request responseString];
        NSMutableString *muString = [[NSMutableString alloc] initWithString:responseString];
        [muString replaceOccurrencesOfString:@"\r" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, muString.length)];
        [muString replaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, muString.length)];
        [muString replaceOccurrencesOfString:@"\t" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, muString.length)];
        NSDictionary *tempDic = [XDTools  JSonFromString:muString];
        
    }];
    
    [request setFailedBlock:^{
       
    }];
    [request startAsynchronous];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
